<?php

namespace App\Http\Controllers\Site\Custom\Purchaser;

use App\B2BProject\Exception\UserTypeException;
use App\B2BProject\Site\HelperControllerBuildTrait;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class MyIssueController extends Controller
{
    use HelperControllerBuildTrait;

    public function getIndex(Request $request)
    {
        if (\Auth::user()->isPurchaser()) {

            if ($request->ajax()) {
                $request->query->set('page', $request->query('page', $request->page));
                $pageSize = $request->query('pageSize', $request->pageSize);

                $allIssues     = \Auth::user()->issues()->paginate($pageSize);
                $repliedIssues = \Auth::user()->issues()->isReplied()->paginate($pageSize);

                $all     = [
                    'detail'         => [],
                    'pagingConfData' => [
                        'page'     => $allIssues->currentPage(),
                        'pageSize' => $pageSize,
                        'total'    => $allIssues->total(),
                    ],
                ];
                $replied = ['detail'         => [],
                            'pagingConfData' => ['page'     => $repliedIssues->currentPage(),
                                                 'pageSize' => $pageSize,
                                                 'total'    => $repliedIssues->total(),
                            ],
                ];

                foreach ($allIssues as $issue) {
                    $all['detail'][] = [
                        'id'           => $issue->id,
                        'goodsName'    => $issue->goods->name,
                        'userName'     => $issue->member->user,
                        'askTime'      => date('Y-m-d H:i:s', strtotime($issue->created_at)),
                        'askContent'   => $issue->content,
                        'replyTime'    => $issue->reply ? date('Y-m-d H:i:s',
                            strtotime($issue->reply->created_at)) : '',
                        'replyContent' => $issue->reply ? $issue->reply->content : '',
                    ];
                }

                foreach ($repliedIssues as $issue) {
                    $replied['detail'][] = [
                        'id'           => $issue->id,
                        'goodsName'    => $issue->goods->name,
                        'userName'     => $issue->member->user,
                        'askTime'      => date('Y-m-d H:i:s', strtotime($issue->created_at)),
                        'askContent'   => $issue->content,
                        'replyTime'    => $issue->reply ? date('Y-m-d H:i:s',
                            strtotime($issue->reply->created_at)) : '',
                        'replyContent' => $issue->reply ? $issue->reply->content : '',
                    ];
                }

                return response()->json(['data' => [$all, $replied]]);
            }

            return view('host/custom/purchaser/my-issue/index');
        }

        throw new UserTypeException;
    }
}
